1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
<script>
import { invalidate } from "$app/navigation";
import { untrack } from "svelte";
import handler from "$lib/handler";
import { onDestroy, onMount } from "svelte";
import { t } from "$lib/translations";
import { goto, invalidateAll } from "$app/navigation";
import { pin } from "$lib/store";
import { enhance } from "$app/forms";
import Amount from "$comp/Amount.svelte";
import Avatar from "$comp/Avatar.svelte";
import Icon from "$comp/Icon.svelte";
import Numpad from "$comp/Numpad.svelte";
import Spinner from "$comp/Spinner.svelte";
import { page } from "$app/stores";
import {
btc,
sat,
post,
back,
f,
toFiat,
s,
sats,
focus,
loc,
} from "$lib/utils";
let { data, form } = $props();
let { trusted, balance, invoice, user } = $state(data);
let { address, hash, payreq, user: recipient, tip } = $state(invoice);
let { currency } = $derived(user);
let locale = $derived(loc(user));
let { amount } = $state(form || invoice);
let rate = $derived(invoice.rate * (data.rate / data.invoiceRate));
let a = $state(),
af = $state(),
fiat = $state(untrack(() => !amount));
let amountFiat = $state((untrack(() => amount) * untrack(() => rate)) / sats);
let setAmount = () => {
amount = a;
amountFiat = af;
};
let submit = $state(),
submitting = $state();
let toggle = () => (submitting = !submitting);
let external = async () => {
let { id } = await post(`/invoice`, {
invoice: {
...invoice,
type: "lightning",
amount,
currency,
},
user: { username: recipient.username },
});
goto(`/invoice/${id}?options=true`, {
invalidateAll: true,
});
};
let toggleTrust = async (e) => {
e.preventDefault();
e.stopPropagation();
if (trusted) {
await post("/post/trust/delete", { id: recipient.id });
} else {
await post("/post/trust", { id: recipient.id });
}
invalidate("app:trust");
};
$effect(() => {
if (form?.message?.includes("pin")) $pin = undefined;
submitting = false;
});
// onMount(async () => {
// if (browser && window.NDEFReader && user.nfc) {
// try {
// let ndef = new NDEFReader();
// await ndef.scan();
//
// ndef.addEventListener('readingerror', (e) => {
// console.log('nfc error', e);
// });
//
// ndef.addEventListener('reading', ({ message, serialNumber }) => {
// console.log(message, serialNumber);
// });
// } catch (e) {
// console.log('NFC error', e);
// }
// }
// });
let setMax = (e) => {
e.preventDefault();
fiat = false;
amount = balance;
};
</script>
{#if form?.message}
<div class="text-red-600 text-center">
{form.message}
</div>
{/if}
<div class="container px-4 max-w-xl mx-auto text-center space-y-5">
{#if amount}
<h1 class="text-4xl font-bold">
{$t("payments.send")}
</h1>
<Amount {amount} {tip} {rate} {currency} {locale} />
<h1 class="text-xl text-secondary">
{$t("payments.to")}
</h1>
<div class="flex p-1 gap-2 justify-center">
<Avatar user={recipient} size={"20"} />
<p class="text-4xl break-words my-auto">
{recipient.username}
</p>
</div>
{#if invoice.items.length}
<h1 class="text-xl md:text-2xl text-secondary">
{$t("payments.for")}
</h1>
{#each invoice.items as i}
<div class="grid grid-cols-4 text-xl">
<div class="mr-auto grow col-span-2">
<span class="mr-2">{i.quantity}</span>
{i.name}
</div>
<div class="font-semibold text-right">
{f(i.price * i.quantity, invoice.currency)}
</div>
<div class="text-secondary text-right text-lg my-auto">
{sat(btc(i.price * i.quantity, invoice.rate))}
</div>
</div>
{/each}
{/if}
{:else}
<Numpad
bind:amount={a}
bind:amountFiat={af}
bind:fiat
{currency}
{rate}
{submit}
/>
{/if}
<form method="POST" use:enhance={handler(toggle)} class="space-y-2">
<input name="address" value={address} type="hidden" />
<input name="amount" value={amount} type="hidden" />
<input name="payreq" value={payreq} type="hidden" />
<input name="username" value={recipient.username} type="hidden" />
<input name="pin" value={$pin} type="hidden" />
<div class="flex w-full text-xl">
{#if amount}
<button
use:focus
type="submit"
class="btn btn-primary"
disabled={submitting}
>
{#if submitting}
<Spinner />
{:else}
{$t("payments.send")}
{/if}
</button>
{:else}
<button
type="button"
class="btn !w-auto grow"
onclick={setMax}
onkeydown={setMax}>Max ⚡️{s(balance)}</button
>
<button
bind:this={submit}
type="button"
class="btn btn-accent !w-auto grow"
onclick={setAmount}
>
{$t("payments.next")}
</button>
{/if}
</div>
<button type="button" class="btn" onclick={external}
>{$t("payments.moreOptions")}</button
>
{#if !trusted}
<button type="button" class="btn" onclick={toggleTrust}>
<iconify-icon icon="ph:star-fill" width={32}></iconify-icon>
{$t("payments.trustUser")}</button
>
{/if}
</form>
</div>
|